package com.platform.controller;

import com.platform.entity.ProCategoryEntity;
import com.platform.service.ProCategoryService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Controller
 *  编辑商品:商品类型
 * @author liweihai
 * @email liweihai12345@sina.com
 * @date 2019-03-29 10:09:37
 */
@RestController
@RequestMapping("category")
public class ProCategoryController {
    @Autowired
    private ProCategoryService proCategoryService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("category:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ProCategoryEntity> tProCategoryList = proCategoryService.queryList(query);
        int total = proCategoryService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(tProCategoryList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("category:info")
    public R info(@PathVariable("id") Integer id) {
        ProCategoryEntity tProCategory = proCategoryService.queryObject(id);

        return R.ok().put("tProCategory", tProCategory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("category:save")
    public R save(@RequestBody ProCategoryEntity tProCategory) {
        proCategoryService.save(tProCategory);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("category:update")
    public R update(@RequestBody ProCategoryEntity tProCategory) {
        proCategoryService.update(tProCategory);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("category:delete")
    public R delete(@RequestParam Integer id) {
        System.out.println(id);
        Integer[] ids = {id};
        proCategoryService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {
        //一级分类列表
        List<ProCategoryEntity> listL1 = new ArrayList<>();
        //二级分类列表
        List<ProCategoryEntity> listL2 = new ArrayList<>();
        //总列表(包含一级二级)
        List<ProCategoryEntity> list = proCategoryService.queryList(params);
        for (ProCategoryEntity proCategoryEntity : list) {
            if(proCategoryEntity.getParentId()==0){
                //当parentId==0,将所有一级的分类放入一级分类列表
                listL1.add(proCategoryEntity);
            }else if(proCategoryEntity.getParentId().toString().equals(params.get("cateL1"))){
                //如果参数catel1不是空的,只取parentId==cateL1的分类放入二级分类列表中
                listL2.add(proCategoryEntity);
            }
        }
        return R.ok().put("list",list).put("listL1", listL1).put("listL2",listL2);
    }
}
